/*
 * Header file for marketSim.c
 * Includes definitions and function declerations
 * 
 */

#define QUEUESIZE 5000

typedef struct {
	long id,oldid;
	long timestamp;
	int vol;
	int price1, price2;
	char action, type;
} order;

struct timeval startwtime, endwtime;

typedef struct {
	order item[QUEUESIZE];
	long head, tail;
	int full, empty;
	pthread_mutex_t *mut;    // fail is a decoy mutex, resolving a segfault
	pthread_cond_t *notFull, *notEmpty;
} queue;


//==========================================================
//---Structure makes every queue visible in every thread----
//---It is used as argument in pthread_create---------------
//========================================================== 
typedef struct{
	queue *q;
	queue *b;
	queue *s;
	queue *bl;
	queue *sl;
	queue *bstop;
	queue *sstop;
	queue *blstop;
	queue *slstop;
	queue *cancel;
}arguments;

// list all threads to be created
void *Prod (void *arg);
void *Cons (void *arg);
void *SStop(void *arg);
void *BStop(void *arg);
void *BStopLimit( void *arg );
void *SStopLimit( void *arg );
void *BuyLimitSellLimit( void *arg );
void *BuyLimitSellMarket( void *arg);
void *BuyMarketSellLimit( void *arg);
void *BuyMarketSellMarket( void *arg );
void *Cancel( void *arg );
void *forceTrans(void *arg);
  

order makeOrder();
inline long getTimestamp();
void dispOrder(order ord);
void trace( long time, int price, int vol, order ord1, order ord2, FILE *f);

queue *queueInit (void);
void queueDelete (queue *q);
void queueAdd (queue *q, order ord);
void queueDel (queue *q, order *ord);
void DeleteMax( queue *q, order *disc);
void DeleteMin( queue *q, order *disc);
void InsertHeapMin( queue *q, order ord);
void InsertHeapMax( queue *q, order ord);
void Transaction( queue *b, queue *s );
void sort_order( order ord, arguments *arg);
int SearchQueue( queue *q,long id);

void extract( queue *q, long i );
void ExtractHeapMin(queue *q,long j );
void ExtractHeapMax(queue *q,long j );

int currentPriceX10 = 1000;
FILE *f;

pthread_mutex_t mut;
pthread_mutex_t file, price,force_mut;
pthread_cond_t  PriceChange, stuck;




